﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFContracts;
using TrainingWorkshop.IDAL;
using TrainingWorkshop.Model;
using System.Transactions;

namespace WCFServices
{
    /// <summary>
    /// Author:Sissi-Zeng
    /// Date:2012-8-28 20:00
    /// Class ProjectBService provides a set of services to client.
    /// In this part, ProjectBService implements IProjectService methods in CourseDetailBU,which is a service contract
    public partial class ProjectBService : IProjectBService
    {
        public int AddStudentsToCourse(int courseId, List<TrainingWorkshop.Model.Student> studentsToAdd)
        {
            ICourseDetailRepository courseDetailRep = factory.CreateCourseDetailRepository();

                if (studentsToAdd != null && studentsToAdd.Count > 0)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        foreach (Student s in studentsToAdd)
                        {
                            courseDetailRep.Insert(courseId, s);
                        }
                        transactionScope.Complete();
                        return 1;
                   }
              }
                return 0;
        }

        /// <summary>
        /// Delete specific students from given course
        /// </summary>
        /// <param name="courseId">given courseId</param>
        /// <param name="studentsToDelete">list of student to delete</param>
        /// <returns>success or fail of delete operation</returns>
        public int DeleteStudentsFromCourse(int courseId, List<TrainingWorkshop.Model.Student> studentsToDelete)
        {
            ICourseDetailRepository courseDetailRep = factory.CreateCourseDetailRepository();
            if (studentsToDelete != null && studentsToDelete.Count > 0)
            {
                using (TransactionScope tranScope = new TransactionScope())
                {
                    foreach (Student s in studentsToDelete)
                    {
                        courseDetailRep.Delete(courseId, s);
                    }
                    tranScope.Complete();
                }
                return 1;
            }
            return 0;
        }
    }
}
